using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Tracker.Mobile.Droid.TrackerWebService;

namespace Tracker.Mobile.Droid.Adapters
{
    public class PositionAdapter : ArrayAdapter<PositionDTO>
    {
        private readonly Context _context;
        private readonly LayoutInflater _inflater;
        private readonly IList<PositionDTO> _positions;

        public PositionAdapter(Context context, IList<PositionDTO> positions)
            : base(context, Resource.Layout.PositionRow, positions)
        {
            _context = context;
            _inflater = _context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
            _positions = positions;
        }

        public override View GetView(int index, View convertView, ViewGroup parent)
        {
            var view = convertView;
            var position = _positions[index];

            if (view == null)
            {
                view = _inflater.Inflate(Resource.Layout.PositionRow, null);
            }

            var date = view.FindViewById<TextView>(Resource.Id.Date);
            var latitude = view.FindViewById<TextView>(Resource.Id.Latitude);
            var longitude = view.FindViewById<TextView>(Resource.Id.Longitude);
            var address = view.FindViewById<TextView>(Resource.Id.Address);

            date.Text = position.InsertDate.ToString("d");
            latitude.Text = position.Latitude.ToString();
            longitude.Text = position.Longitude.ToString();
            address.Text = String.Format("{0} {1} {2} {3}", position.Address1, position.City, position.State, position.Zip);

            return view;
        }
    }
}